from __future__ import annotations

from dataclasses import dataclass


@dataclass
class MemberStrength:
    stress_MPa: float
    utilization: float
    ok: bool
    note: str


def check_member_strength(axial_kN: float, area_mm2: float, moment_kNm: float = 0.0, section_modulus_mm3: float = 1e6, design_strength_MPa: float = 215.0) -> MemberStrength:
    sigma_N = (axial_kN * 1e3) / max(1.0, area_mm2)
    sigma_M = (moment_kNm * 1e6) / max(1.0, section_modulus_mm3)
    sigma = abs(sigma_N) + abs(sigma_M)
    u = sigma / max(1e-6, design_strength_MPa)
    return MemberStrength(stress_MPa=sigma, utilization=u, ok=u <= 1.0, note="轴力+弯矩强度")


@dataclass
class MemberStability:
    slenderness: float
    capacity_kN: float
    ok: bool
    note: str


def check_member_stability(effective_length_mm: float, radius_gyration_mm: float, axial_design_kN: float, phi_e: float = 0.5, f_y_MPa: float = 215.0, area_mm2: float = 1000.0) -> MemberStability:
    slenderness = effective_length_mm / max(1e-6, radius_gyration_mm)
    N_cr = phi_e * f_y_MPa * area_mm2 / 1000.0
    ok = axial_design_kN <= N_cr and slenderness <= 180.0
    return MemberStability(slenderness=slenderness, capacity_kN=N_cr, ok=ok, note="简化稳定承载力")


